package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    //新增
    @PostMapping("/cart/add")
    public ResultInfo save(@RequestBody Cart cart){
        Cart cartFromDB = cartService.save(cart);

        //之所以要返回这个cart对象,是因为前端需要从这个对象中获取number来显示
        return ResultInfo.success(cartFromDB);
    }

    //查询
    @GetMapping("/cart/list")
    public ResultInfo findList() {
        List<Cart> cartList = cartService.findList();
        return ResultInfo.success(cartList);
    }


    //清空购物车
    @DeleteMapping("/cart/clean")
    public ResultInfo clean(){
        cartService.clean();
        return ResultInfo.success(null);
    }
}